import br.com.nooclix.salon.ui.dto.person.AddressTO;
import br.com.nooclix.salon.ui.dto.person.CityTO;
import br.com.nooclix.salon.ui.dto.person.PersonTO;
import br.com.nooclix.salon.ui.dto.person.StateTO;
import br.com.nooclix.salon.ui.util.Utilitys;
import br.com.nooclix.salon.ui.util.WindowsMessage;

import mx.controls.Alert;
import mx.controls.ComboBox;
import mx.events.DropdownEvent;

public var person:PersonTO;
public var address:AddressTO;

public function getPerson():PersonTO{

	if(person == null){
		person = new PersonTO();
		address = new AddressTO();
		person.address = address;
		
	}else{
		
		address = person.address;
	}
	
	person.name = fieldName.text;
	person.email = fieldEmail.text;
	person.cellphone = fieldCellphone.text;
	person.cpf = fieldCPF.text;
	person.phone = fieldPhone.text;
	
	address.cep = fieldCEP.text;
	address.city = cbCity.selectedItem as CityTO;
	address.neighborhood = fieldNeig.text;
	address.number = fieldNumber.text;
	address.street = fieldStreet.text;
	
	if(radioM.selected){
		person.sex = "M";
	}else{
		person.sex = "F";
	}
	
	return person;
	
}


/*|| 
	fieldCPF.text == "22222222222"||
	fieldCPF.text == "33333333333"||
	fieldCPF.text == "44444444444"||
	fieldCPF.text == "55555555555"||
	fieldCPF.text == "66666666666"||
	fieldCPF.text == "77777777777"||
	fieldCPF.text == "88888888888"||
	fieldCPF.text == "99999999999"||
	fieldCPF.text == "00000000000"||
*/
public function setPerson(p:PersonTO):void{
	clearFields();
	person = p;
	
	fieldName.text = p.name;
	fieldCellphone.text = p.cellphone;
	fieldCPF.text = p.cpf;
	fieldEmail.text = p.email;
	fieldPhone.text = p.phone;
	
	Utilitys.selectItem(person.address.city.state.code.toString(), cbState, "code");
	
	var cb:ComboBox = cbState;
	cb.dispatchEvent(new DropdownEvent(DropdownEvent.CLOSE));
	
	if(p.sex == "M"){
		radioM.selected = true;
		radioF.selected = false;
	}else{
		radioF.selected = true;
		radioM.selected = false;
	}
	
	fieldCEP.text = p.address.cep;
	fieldStreet.text = p.address.street;
	fieldNumber.text = p.address.number;
	fieldNeig.text = p.address.neighborhood;

}

private function getStates():void{
	personRemote.listState();
}

private function loadCityOfEntity():void{
	
	//Seleciona a cidade correto no combobox
	Utilitys.selectItem(person.address.city.code.toString(), cbCity, "code");
	
}

public function clearFields():void{

	fieldCellphone.text="";
	fieldCEP.text="";
	fieldCPF.text="";
	fieldEmail.text="";
	fieldName.text="";
	fieldNeig.text="";
	fieldNumber.text="";
	fieldPhone.text="";
	fieldStreet.text="";
	fieldName.setFocus();
	
	tabPerson.selectedIndex = 0;
	cbState.selectedIndex = 0;
	radioF.selected = true;
	
	var cb:ComboBox = cbState;
	cb.dispatchEvent(new DropdownEvent(DropdownEvent.CLOSE));
	person = null;
	address = null;
	
}

private function getCitiesBySelectedState():void{
	personRemote.findCitiesByState(cbState.selectedItem as StateTO);
}